package com.my.blogs.personblog.Service;

import lombok.Getter;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.repository.query.Param;

import java.io.InputStream;

@Getter
public class NlpService {
    private final SentenceDetectorME sentenceDetector;
    private final TokenizerME tokenizer;
    private final POSTaggerME posTagger;
    private static final String[] CREATE_KEYWORDS = {"create", "add", "new"};
    private static final String[] READ_KEYWORDS = {"read", "get", "find", "search"};
    private static final String[] UPDATE_KEYWORDS = {"update", "edit", "change"};
    private static final String[] DELETE_KEYWORDS = {"delete", "remove", "erase"};

    public NlpService() {
        try (InputStream modelIn = this.getClass().getResourceAsStream("/models/en-sent.bin")) {
            SentenceModel sentenceModel = new SentenceModel(modelIn);
            sentenceDetector = new SentenceDetectorME(sentenceModel);
        } catch (Exception e) {
            // 异常处理
            throw new RuntimeException("Failed to load sentence detection model", e);
        }

        // 初始化分词器
        try (InputStream modelIn = this.getClass().getResourceAsStream("/models/en-token.bin")) {
            TokenizerModel tokenModel = new TokenizerModel(modelIn);
            tokenizer = new TokenizerME(tokenModel);
        } catch (Exception e) {
            // 异常处理
            throw new RuntimeException("Failed to load tokenizer model", e);
        }

        // 初始化词性标注器
        try (InputStream modelIn = this.getClass().getResourceAsStream("/models/en-pos-maxent.bin")) {
            POSModel posModel = new POSModel(modelIn);
            posTagger = new POSTaggerME(posModel);
        } catch (Exception e) {
            // 异常处理
            throw new RuntimeException("Failed to load POS tagger model", e);
        }
    }

    public String[] detectSentences(String text) {
        return sentenceDetector.sentDetect(text);
    }

    public String[] tokenizeSentence(String sentence) {
        return tokenizer.tokenize(sentence);
    }

    public String[] tagPOS(String[] tokens) {
        return posTagger.tag(tokens);
    }
    /**
     * 解析命令并确定CRUD操作类型
     *
     * @param command 用户输入的命令
     * @return 对应的CRUD操作类型
     */
    public String parseCommand(String command) {
        command = command.toLowerCase().trim();

        if (containsKeyword(command, CREATE_KEYWORDS)) {
            return "CREATE";
        } else if (containsKeyword(command, READ_KEYWORDS)) {
            return "READ";
        } else if (containsKeyword(command, UPDATE_KEYWORDS)) {
            return "UPDATE";
        } else if (containsKeyword(command, DELETE_KEYWORDS)) {
            return "DELETE";
        } else {
            return "UNKNOWN";
        }
    }
    /**
     * 检查命令中是否包含特定的关键词
     *
     * @param command   用户输入的命令
     * @param keywords  关键词数组
     * @return 如果命令中包含任一关键词，则返回true
     */
    @org.jetbrains.annotations.Contract(pure = true)
    private boolean containsKeyword(String command, String @NotNull [] keywords) {
        for (String keyword : keywords) {
            if (command.contains(keyword)) {
                return true;
            }
        }
        return false;
    }
}
